
const db = require( "../models" );
const logger = require( "../utils/utils.logger" );
const tools = require( "../utils/utils.tools" );

const DAO = require( "../dao/DAO" );
const experience = db.experience;


// Retrieve all comment from the database.
exports.list = ( req, res ) => {
    const pm = req.body;
    
    DAO.list( experience, pm, list => {
        //logger.debug(`${req.method} ${req.baseUrl + req.path} *** 参数：${JSON.stringify(pm)}; 响应：${JSON.stringify(list)}`);
  
        res.sendResult( list )
    } )
};

exports.getAllList = function ( pm, cb ) {
    //获取资历
    DAO.list( experience, pm, list => {
         cb( list )
     } )
}
exports.create = ( req, res ) => {
    const pm = req.body;
    // 请求验证
    if ( !pm.start_time ) return res.sendResult( {
        data: '',
        code: 605,
        message: "开始时间不能为空"
    } )
    
    // Create a Cate
    const createData = {
        start_time:  tools.timestampToTime( pm.start_time ),
        //end_time: pm.end_time ? tools.end_time,
        unit: pm.unit,
        position: pm.position,
        type: pm.type,
    };
    if ( pm.end_time ) { 
        createData.end_time = tools.timestampToTime( pm.end_time )
    }
     DAO.create( experience, createData, data => {
         //logger.info(`${req.method} ${req.baseUrl + req.path} *** 参数：${JSON.stringify(pm)}; 响应：${JSON.stringify(data)}`);
         res.sendResult( data )
     } )
    /*experience.findAll( {
        where: {
            'type': pm.type
        }
    }).then( list => {
        console.log( list )
        const startTime = new Date( pm.start_time ).getTime();
        const endTime = new Date( pm.end_time ).getTime();
        let flag = true;
        for ( let i = 0; i < list.length; i++ ) { 
            let st = new Date( list[i].start_time ).getTime();
            let et = new Date( list[i].end_time ).getTime();
            if ( endTime < st || startTime > et ) {} else {
                flag = false;
                break;
            }
        }
        let typeName = pm.type == 1 ? '工作' :'教育'
        if ( flag == false ) return res.sendResultAto( null, 605, typeName +'所选时间段已存在！' )
    
        
    } )*/
};


exports.update = ( req, res ) => {
    const pm = req.body;
    // 请求验证
    if ( !pm.id ) return res.sendResult( {
        data: '',
        code: 605,
        message: "ID不能为空！"
    } )
    pm.start_time ? pm.start_time = tools.timestampToTime( pm.start_time ) : ''
    pm.end_time ? pm.end_time = tools.timestampToTime( pm.end_time ) : ''
    DAO.update( experience, pm, {
        id: pm.id
    }, data => {
        res.sendResult( data )
    } )

};
exports.delete = ( req, res ) => {
    const pm = req.body;
    // 请求验证
    if ( !pm.id ) return res.sendResult( {
        data: '',
        code: 605,
        message: "ID不能为空！"
    } )
    DAO.delete( experience, {
        id: pm.id
    }, data => {
        res.sendResult( data )
    } )
};
